from aiohttp import web
import json

async def parse_request(request: web.Request) -> dict:
    """统一解析请求数据"""
    if request.method == "GET":
        return dict(request.query)
    try:
        return await request.json()
    except json.JSONDecodeError:
        raise web.HTTPBadRequest(text="Invalid JSON format")

def validate_params(data: dict, required: list) -> None:
    """参数校验"""
    missing = [field for field in required if field not in data]
    if missing:
        raise web.HTTPBadRequest(
            text=json.dumps({"error": f"Missing parameters: {', '.join(missing)}"}),
            content_type='application/json'
        )